<?php

/*
 * Edit by shengxin
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */

namespace api\mm\controller;

use cmf\controller\RestBaseController;
use api\mm\model\ClientModel;
use api\mm\model\AppInfoModel;
use api\mm\model\AppSettingModel;
use api\mm\model\TimeSettingModel;
use think\facade\Db;

/**
 * Description of ParentController
 *
 * @author PC
 */
class ParentController extends RestBaseController{
    
    public function getChildList(){
        $model = new ClientModel();
        $data = $model->alias('c')
                ->field('c.*')
                ->join('pcmap m','m.child_id=c.client_id')
                ->where('m.id',1)->select();
        $this->success('ok',$data);
    }
    
    private function getClientId(){
        $cdata = Db::name('pcmap')->where('parent_id',1)->where('is_default',1)->find();
        if($cdata){
            return $cdata['child_id'];
        }
        $this->error('no client');
        return false;
    }
    
    private function timeFormat($num){
        $min = floor($num/60);
        $sec = ($num%60);
        return ($min>10?'':'0').$min.':'.($sec>10?'':'0').$sec;
    }
    
    public function getTimeSetting(){
        $cid = $this->getClientId();
        $model = new TimeSettingModel();
        $data = $model->where('client_id',$cid)->select();
        $list = [];
        foreach ($data as $val){
            $d = [];
            $d['id']=$val['id'];
            $d['type']=$val['type'];
            $d['day']=$val['day'];

            $d['start']=$this->timeFormat($val['time_start']);
            $d['end']=$this->timeFormat($val['time_end']);
            $list[] = $d;
        }
        $this->success('ok',$list);
    }
    
    public function addTimeSetting(){
        $cid = $this->getClientId();
        $data = $this->request->post();
        $model = new TimeSettingModel();
        $data['client_id'] = $cid;
        $model->insert($data);
    }
    
    public function getApplist(){
        $cid = $this->getClientId();
        $model = new AppInfoModel();
        $applist = $model->where('client_id',$cid)->select();
        $this->success('ok',$applist);
    }
    
    public function getAppSetting(){
        $cid = $this->getClientId();
        $model = new AppSettingModel();
        $applist = $model->where('client_id',$cid)->select();
        $this->success('ok',$applist);
    }
    
    
    public function setAppSetting(){
        $cid = $this->getClientId();
        $app = $this->request->post();
        $appid = $app['id'];
        unset($app['id']);
        $model = new AppSettingModel();
        $model->where('client_id',$cid)->where('id',$appid)->update($app);
        $this->success('ok');
    }
}
